package com.example.demo.thread.lock;

/**
 * Synchronized 关键字测试
 *
 * @author luowj12
 * @since 2021/4/13 16:56
 */
public class SynchronizedTest {

    public synchronized void test1(String threadId) {
        System.out.println(threadId+"-----开始锁----");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(threadId+"-----结束锁----");
    }

    public void test2() {
        synchronized(this) {

        }
    }

    public static void main(String[] args){
        SynchronizedTest synchronizedTest = new SynchronizedTest();
        new Thread(){
            public void run() {
                synchronizedTest.test1(Thread.currentThread().getName());
            };
        }.start();

        new Thread(){
            public void run() {
                synchronizedTest.test1(Thread.currentThread().getName());
            };
        }.start();
    }
}
